package com.bestcem.xm.user.grpc.client;

import com.bestcem.xm.user.service.dto.user.IpInfoDTO;
import com.bestcem.xm.user.util.Ip2RegionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * ip服务
 *
 * @author wangyunyun <yunyun.wang@idiaoyan.com>
 * @version v1.0
 * @date 2022/3/1 14:30
 **/
@Slf4j
@Service("userIpGrpcService")
public class IpGrpcService {

    //@GrpcClient(GrpcClientNames.IP)
    //private IpServiceGrpc.IpServiceBlockingStub ipServiceBlockingStub;
    //
    //@Value("${grpcService.ip.deadline}")
    //private long deadline;
    //
    //@Autowired
    //private TokenCallCredentials tokenCallCredentials;
    //
    //@Resource
    //private SettingProperties settingProperties;

    /**
     * ipinfo proto to ipinfo pojo
     *
     * @param pIp ip信息
     * @return IpInfoDTO
     * @author wangyunyun <yunyun.wang@idiaoyan.com>
     * @date 2022/3/3 11:58
     */
    //private IpInfoDTO proto2Pojo(IpInfo pIp) {
    //    IpInfoDTO ipInfo = new IpInfoDTO();
    //    ipInfo.setIp(pIp.getIp());
    //    ipInfo.setCountry(pIp.getCountry());
    //    ipInfo.setProvince(pIp.getProvince());
    //    ipInfo.setCity(pIp.getCity());
    //    ipInfo.setDistrict(pIp.getDistrict());
    //    ipInfo.setIsp(pIp.getIsp());
    //    return ipInfo;
    //}

    /**
     * @param ip ip地址
     * @return IpInfoDTO
     * @author wangyunyun <yunyun.wang@idiaoyan.com>
     * @date 2022/3/3 11:57
     */
    public IpInfoDTO getIpInfo(String ip) {
        try {
            //中国|江苏省|苏州市|电信
            String cityInfo = Ip2RegionUtil.getCityInfo(ip);
            String[] cityList = cityInfo.split("\\|");
            IpInfoDTO ipInfo = new IpInfoDTO();
            ipInfo.setIp(ip);
            ipInfo.setCountry(cityList[0]);
            ipInfo.setProvince(cityList[1]);
            ipInfo.setCity(cityList[2]);
            ipInfo.setIsp(cityList[3]);
            return ipInfo;
        } catch (Exception e) {
            return null;
        }
        //// 基线环境直接返回
        //if (settingProperties.isBaseLine()) {
        //    return null;
        //}
        //
        //if (StringUtils.isBlank(ip)) {
        //    log.error("gRPC getIpInfo param error. ip is blank");
        //    return null;
        //}
        //try {
        //    SingleIpRequest.Builder request = SingleIpRequest.newBuilder();
        //    request.setIp(ip);
        //    // token认证
        //    SingleIpResponse response = ipServiceBlockingStub.withCallCredentials(tokenCallCredentials)
        //            .withDeadlineAfter(deadline, TimeUnit.MILLISECONDS).get(request.build());
        //
        //    if (response.getStatus().getCode() == Code.ERROR) {
        //        log.error("gRPC ip error,{}", response.getStatus().getMessage());
        //        return null;
        //    }
        //    return proto2Pojo(response.getIpInfo());
        //} catch (Exception e) {
        //    log.error("gRPC getIpInfo error", e);
        //}
        //return null;
    }
}
